package tech.six6.controller;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.jupiter.api.Test;
import tech.six6.dao.MybatisDemoDao;
import tech.six6.entiy.MybatisDemo;
import tech.six6.service.MybatisDemoService;
import tech.six6.service.impl.MybatisDemoServiceImpl;

import java.io.IOException;
import java.io.Reader;
import java.util.List;

public class MybatisDemoController {

    private MybatisDemoService demoService = new MybatisDemoServiceImpl();

    @Test
    public void insert(){
        MybatisDemo demo = new MybatisDemo();
        demo.setName("张三");
        demo.setPassword("123456");
        demo.setSex("男");
        int i = demoService.add(demo);
        if (i>0){
            System.out.println("插入成功"+i);
        }else{
            System.out.println("插入失败"+i);
        }
    }

    @Test
    public void delete(){
        int id = 1;
        if (demoService.delete(id) > 0){
            System.out.println("删除成功"+id);
        }else{
            System.out.println("删除失败"+id);
        }
    }

    /**
     * 更新id号不存在的
     */
    @Test
    public void update1(){
        MybatisDemo demo = new MybatisDemo();
        demo.setId(1);
        demo.setName("更新");
        if (demoService.update(demo)>0){
            System.out.println("更新成功");
        }else{
            System.out.println("更新失败");
        }
    }

    /**
     * 更新id号存在的但是不给能更新的参数的
     */
    @Test
    public void update2(){
        MybatisDemo demo = new MybatisDemo();
        demo.setId(11);
        if (demoService.update(demo)>0){
            System.out.println("更新成功");
        }else{
            System.out.println("更新失败");
        }
    }

    /**
     * 更新id号存在的并且给能更新的参数
     */
    @Test
    public void update3(){
        MybatisDemo demo = new MybatisDemo();
        demo.setId(11);
        demo.setName("更新");
        if (demoService.update(demo)>0){
            System.out.println("更新成功");
        }else{
            System.out.println("更新失败");
        }
    }

    @Test
    public void searchById(){
        int id = 1;
        MybatisDemo demo = demoService.selectById(id);
        System.out.println(demo);
    }

    @Test
    public void searchAll(){
        List<MybatisDemo> demoList = demoService.selectAll();
        demoList.forEach(x-> System.out.println(x));
    }

    public static void main(String[] args) {
        MybatisDemoService demoService = new MybatisDemoServiceImpl();
        MybatisDemo demo = new MybatisDemo();
        demo.setName("李四");
        demo.setPassword("123456");
        demo.setSex("男");
        int i = demoService.add(demo);
        if (i>0){
            System.out.println("插入成功"+i);
        }else{
            System.out.println("插入失败"+i);
        }
    }

}
